package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/dictionary")
public class SystemDictionaryController {
	
	@Autowired
	private ISystemDictionaryService dictionaryService;

	@RequiresPermissions(value = {"dictionary:list","字典列表"},logical = Logical.OR)
	@RequestMapping("list")
	public String list(Model model, @ModelAttribute("qo")QueryObject qo){
		//int i=1/0;
		model.addAttribute("result", dictionaryService.query(qo));
		return "/dictionary/list";
	}


	@RequiresPermissions(value = {"dictionary:saveOrUpdate","字典编辑"},logical = Logical.OR)
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Object saveOrUpdate(SystemDictionary entity){
		JSONResult result = new JSONResult();
		try {
			//int i = 1/0;
			if(entity.getId() != null){
                dictionaryService.updateByPrimaryKey(entity);
            }else{
                dictionaryService.insert(entity);
            }
		} catch (Exception e) {
			e.printStackTrace();
			result.mark(e.getMessage());
		}

		return result;
	}
	
}
